package com.longrise.android.byjk.plugins.tabfirst.introduce;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.plugins.dealsituation.course.video2.media.IjkVideoView;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.utils.PrintLog;

import tv.danmaku.ijk.media.player.IMediaPlayer;

/**
 * Created by callxkj on 2017/11/27.
 * 介绍保易健康的视频类
 */

public class IntroduceVideoActivity extends BaseActivity2 implements View.OnClickListener,
        View.OnTouchListener, SeekBar.OnSeekBarChangeListener, IMediaPlayer.OnPreparedListener,
        IMediaPlayer.OnCompletionListener, IMediaPlayer.OnErrorListener, IMediaPlayer.OnInfoListener {

    public static final String VIDEO_URL = "videourl";
    public static final String VIDEO_NAME = "videoname";
    private static final String TAG = "IntroduceVideoActivity";

    private IjkVideoView mPlayer;

    /**
     * 播放器布局
     */
    private View mControllerLayout;
    private View mTitleLayout;
    private RelativeLayout mBack;
    private TextView mTvTitle;


    /**
     * 控制栏
     */
    private ImageButton mBtnPause;
    private TextView mTvPosition;
    private TextView mTvDuration;
    private SeekBar mProgress;
    private boolean mDrag;


    /**
     * 缓冲或者出错
     */
    private View mHaze;
    private ProgressBar mPbLoading;
    private TextView mTvError;
    private String mVideoUrl;
    private String mVideoName;


    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        if (savedInstanceState != null) {
            mVideoName = savedInstanceState.getString(VIDEO_NAME);
            mVideoUrl = savedInstanceState.getString(VIDEO_URL);
        } else {
            getIntentData();
        }
        return R.layout.activity_introducevideo;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(VIDEO_URL,mVideoUrl);
        outState.putString(VIDEO_NAME,mVideoName);
    }


    @Override
    public void beforeSetContentView() {
        initTheme();
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    public void initView() {
        setToolbarVisible(false);

        mPlayer = (IjkVideoView) findViewById(R.id.pv_video);
        mControllerLayout = findViewById(R.id.video_controller_layout);
        mTitleLayout = findViewById(R.id.video_title_layout);

        //title栏
        mBack = (RelativeLayout) findViewById(R.id.rl_back_left);
        mTvTitle = (TextView) findViewById(R.id.tv_title);

        //controller 栏
        mBtnPause = (ImageButton) findViewById(R.id.btn_pause);
        mTvPosition = (TextView) findViewById(R.id.tv_position);
        mTvDuration = (TextView) findViewById(R.id.tv_duration);
        mProgress = (SeekBar) findViewById(R.id.sb_progress);

        mHaze = findViewById(R.id.video_haze);
        mPbLoading = (ProgressBar) findViewById(R.id.pb_loading);
        mTvError = (TextView) findViewById(R.id.tv_showerror);

        regEvent(true);
        openMedia(true);
        startPlayer();

    }

    @Override
    public void onToolbarBackClick() {
       finish();
    }

    private void getIntentData(){
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        mVideoUrl = extras.getString(VIDEO_URL);
        mVideoName = extras.getString(VIDEO_NAME);
    }

    private void regEvent(boolean isClick) {
        if (mBtnPause != null) {
            mBtnPause.setOnClickListener(isClick ? this : null);
        }
        if (mBack != null) {
            mBack.setOnClickListener(isClick ? this : null);
        }
        if (mTvError != null) {
            mTvError.setOnClickListener(isClick ? this : null);
        }
        if (mProgress != null) {
            mProgress.setOnTouchListener(isClick ? this : null);
            mProgress.setOnSeekBarChangeListener(isClick ? this : null);
            mProgress.setMax(1000);
        }
        if (mControllerLayout != null) {
            mControllerLayout.setOnClickListener(isClick ? this : null);
        }
        if (mTitleLayout != null) {
            mTitleLayout.setOnClickListener(isClick ? this : null);
        }
        if (mPlayer != null) {
            mPlayer.setOnTouchListener(isClick ? this : null);
        }
    }




    private void openMedia(boolean isClick) {
        if (mPlayer == null) {
            return;
        }
        mPlayer.setOnPreparedListener(isClick ? this : null);
        mPlayer.setOnCompletionListener(isClick ? this : null);
        mPlayer.setOnErrorListener(isClick ? this : null);
        mPlayer.setOnInfoListener(isClick ? this : null);
    }


    /**
     * 加载video资源
     */
    private void startPlayer() {
        if (mPlayer != null && !TextUtils.isEmpty(mVideoUrl)) {
            PrintLog.d(TAG,"mVideoUrl:"+mVideoUrl);
            mPlayer.setVideoPath(mVideoUrl);
        }
    }


    //-----------------setOnClickListener------------------
    @Override
    public void onClick(View v) {

    }
    //----------------------------------------------------


    //-----------------setOnTouchListener------------------
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return false;
    }
    //----------------------------------------------------


    //----------------setOnSeekBarChangeListener------------
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {

    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {

    }
    //-----------------------------------------


    //------------setOnPreparedListener -------
    @Override
    public void onPrepared(IMediaPlayer iMediaPlayer) {

    }
    //-----------------------------------------

    //------------setOnCompletionListener -------
    @Override
    public void onCompletion(IMediaPlayer iMediaPlayer) {

    }
    //-----------------------------------------

    //------------setOnErrorListener ------------
    @Override
    public boolean onError(IMediaPlayer iMediaPlayer, int i, int i1) {
        return false;
    }
    //-----------------------------------------

    //------------setOnInfoListener ------------
    @Override
    public boolean onInfo(IMediaPlayer iMediaPlayer, int i, int i1) {
        return false;
    }
    //-----------------------------------------
}
